package com.sgolub.commanndsonline.web;
   
import java.security.Principal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.WebRequest;

import com.sgolub.commanndsonline.domain.entity.AdvertisingRegistry;
import com.sgolub.commanndsonline.domain.ref.distinct.RefDistinctLocality;
import com.sgolub.commanndsonline.domain.ref.distinct.RefDistinctRegion;
import com.sgolub.commanndsonline.domain.ref.distinct.RefDistinctSupportType;
import com.sgolub.commanndsonline.helpers.JsonFlexGridDataWrapper;
import com.sgolub.commanndsonline.helpers.JsonJQGridDataWrapper;
import com.sgolub.commanndsonline.helpers.ReferenceItem;
import com.sgolub.commanndsonline.helpers.SecurityHelper;
import com.sgolub.commanndsonline.json.ui.UIMenu;
import com.sgolub.commanndsonline.service.entity.AdvertisingRegistryService;
import com.sgolub.commanndsonline.service.ref.distinct.RefDistinctLocalityService;
import com.sgolub.commanndsonline.service.ref.distinct.RefDistinctRegionService;
import com.sgolub.commanndsonline.service.ref.distinct.RefDistinctSupportTypeService;
 
@Controller
@RequestMapping("/public")
public class PublicJSONController {   
	
	@Autowired  
    private AdvertisingRegistryService advertisingRegistryService;
	
	@Autowired
	private RefDistinctRegionService refDistinctRegionService;
	
	@Autowired
	private RefDistinctLocalityService refDistinctLocalityService;
	
	@Autowired
	private RefDistinctSupportTypeService refDistinctSupportTypeService;
	
    
    @RequestMapping(value="/registry", method = RequestMethod.POST)
	public @ResponseBody JsonJQGridDataWrapper<AdvertisingRegistry> getAdvertisinRegistryListPOSTJQGrid(WebRequest request) {    
    	System.out.println("== PublicJSONController.getAdvertisinRegistryListPOSTJQGrid ==");
    	HashMap<String, String> filtersMap = new HashMap<String, String>();
    	
    	//debugRequest(request);
    	
    	filtersMap.put("supportType", 	request.getParameter("filters[supportType]"));
    	filtersMap.put("regionName", 	request.getParameter("filters[regionName]"));
    	filtersMap.put("localityName", 	request.getParameter("filters[localityName]"));
    	filtersMap.put("addresString", 	request.getParameter("filters[addresString]"));
    	filtersMap.put("light", 		request.getParameter("filters[light]"));
    	filtersMap.put("priceFrom", 	request.getParameter("filters[priceFrom]"));
    	filtersMap.put("priceTo", 		request.getParameter("filters[priceTo]"));
    	
    			 
    	int rowsPerPage 		 	= Integer.parseInt(request.getParameter("rows"));
    	int currentPage				= Integer.parseInt(request.getParameter("page"));
    	String sortByColumn			= request.getParameter("sidx");
    	String sortMode				= request.getParameter("sord");

    	List<AdvertisingRegistry> result = advertisingRegistryService.advansedListWithPaginationJQGrid
		(rowsPerPage, currentPage, sortByColumn, sortMode, filtersMap);
    	
    	System.out.println("result.size() :" + 	result.size());
    	System.out.println("rowsPerPage :" + 	rowsPerPage);
    	int totalPages = result.size()/rowsPerPage + (result.size()%rowsPerPage>0?1:0);
    	
    	System.out.println("totalPages :" + totalPages);
    	
    	if (currentPage > totalPages) currentPage = 1;
    	int vFromIndex = rowsPerPage * (currentPage-1);
    	int vToIndex = (rowsPerPage * currentPage >result.size())?(result.size()):(rowsPerPage * currentPage);
    	
    	JsonJQGridDataWrapper<AdvertisingRegistry> jdw = 
    			new JsonJQGridDataWrapper<AdvertisingRegistry>(
    					totalPages,
    					currentPage,
    					result.size(),
    					result.subList(vFromIndex, vToIndex )); 
    	
    	System.out.println("RESULT SIZE IS :" + result.size());
    	 return jdw;  
    }  
    
     
    @RequestMapping(value="/distinct/region", method = RequestMethod.GET)
	public @ResponseBody List<RefDistinctRegion> getRefDistinctRegionList(WebRequest request) {    
    	//System.out.println("GET - ARRAY SIZE IS:" + refDistinctRegionService.list().size());
    	System.out.println("getRefDistinctRegionList term:" + request.getParameter("term"));
    	  
    	return refDistinctRegionService.match(request.getParameter("term"));  
    }
     
    @RequestMapping(value="/distinct/locality", method = RequestMethod.GET)
	public @ResponseBody List<RefDistinctLocality> getRefDistinctLocalityList(WebRequest request) {    
    	//System.out.println("GET - ARRAY SIZE IS:" + refDistinctRegionService.list().size());
    	System.out.println("getRefDistinctLocalityList term:" + request.getParameter("term"));
    	   
    	 return refDistinctLocalityService.match(request.getParameter("term"));  
    }      
    
    
    @RequestMapping(value="/distinct/supportType", method = RequestMethod.GET)
	public @ResponseBody List<RefDistinctSupportType> getRefDistinctSupportTypeList(WebRequest request) {    
    	//System.out.println("GET - ARRAY SIZE IS:" + refDistinctRegionService.list().size());
    	System.out.println("getRefDistinctSupportTypeList term:" + request.getParameter("term"));
    	   
    	 //return refDistinctSupportTypeService.match(request.getParameter("term"));  
    	return refDistinctSupportTypeService.match(request.getParameter("term"));  
    }    
    
   
    
    private void debugRequest(WebRequest request){
       	System.out.println(request.getParameterMap().toString());
    	System.out.println("sord :" + request.getParameter("sord"));
    	System.out.println("page :" + request.getParameter("page")); 
    	System.out.println("nd :" 	+ request.getParameter("nd")); 
    	System.out.println("sidx :" + request.getParameter("sidx")); 
    	System.out.println("_search :" + request.getParameter("_search")); 
    	System.out.println("rows :" + request.getParameter("rows"));
    	System.out.println("filters :" + request.getParameter("filters"));
    	System.out.println("filters[supportType] :" + request.getParameter("filters[supportType]"));
    	System.out.println("filters[regionName] :" + 	request.getParameter("filters[regionName]"));
    	System.out.println("filters[localityName] :" + 	request.getParameter("filters[localityName]"));
    	System.out.println("filters[addresString] :" + 	request.getParameter("filters[addresString]"));
    	System.out.println("filters[light] :" + 		request.getParameter("filters[light]"));
    	System.out.println("filters[priceFrom] :" + 	request.getParameter("filters[priceFrom]"));
    	System.out.println("filters[priceTo] :" + 		request.getParameter("filters[priceTo]"));	
    }

}



